#include "wizard.h"
#include "ui_wizard.h"
#include <QTreeWidgetItem>
#include <QInputDialog>

Wizard::Wizard(QWidget *parent) :
    QDialog(parent),
    m_ui(new Ui::Wizard)
{
    m_ui->setupUi(this);
    db = new CDBProvider("",this);
    loadTree();
    loadSchools();
}

Wizard::~Wizard()
{
    delete m_ui;
}

void Wizard::changeEvent(QEvent *e)
{
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void Wizard::loadTree(){

    QTreeWidgetItem * i = new QTreeWidgetItem();
    i->setText(0,trUtf8("Разделы"));
    i->setIcon(0,QIcon(":/images/mac/fileopen.png"));
    i->setText(1,"0");
    i->setText(2,"topCat");
    m_ui->treeWidget->clear();
    m_ui->treeWidget->addTopLevelItem(i);
    i->addChildren(db->getChieldFromDB(0,false));
    i->setExpanded(true);
}

void Wizard::loadSchools(){
    m_ui->comboBox->addItems(db->getSchools());
}

int Wizard::getCount(){
    return m_ui->spinBox->value();
}

QString Wizard::getNameGroup(){
    return m_ui->lineEdit_2->text();
}
QString Wizard::getDate(){
    return m_ui->calendarWidget->selectedDate().toString(Qt::ISODate);
}

int Wizard::getIdGroup(){
    QTreeWidgetItem *i = m_ui->treeWidget->currentItem();
    if(i->text(2) == "task"){
        i = i->parent();
    }
    return i->text(1).toInt();
}

int Wizard::getIdSchool(){
    return db->getSchool(m_ui->comboBox->currentText());
}

void Wizard::on_pushButton_clicked()
{
    bool ok;
    QString newname = QInputDialog::getText(this,trUtf8("Школа"),trUtf8("введите название новой школы"),QLineEdit::Normal,m_ui->comboBox->currentText(),&ok);
    if(ok){
         db->renameSchool(m_ui->comboBox->currentText(),newname);
         int index = m_ui->comboBox->currentIndex();
         m_ui->comboBox->removeItem(index);
         m_ui->comboBox->insertItem(index,newname);
    }
}
